package com.coder4j.platform.controller;

import com.coder4j.platform.cache.DictCache;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.BaseController;
import com.coder4j.platform.entity.SysDictEntity;
import com.coder4j.platform.mapper.SysDictMapper;
import com.coder4j.platform.service.SysDictService;
import com.coder4j.platform.vo.SysDictQuery;
import com.coder4j.platform.vo.SysDictVO;
import com.coder4j.platform.vo.SysDictEO;
import com.coder4j.starter.web.api.ApiNote;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.util.List;

/**
 * 系统字典
 * @author keller
 * @date 2025-05-30 21:19:10
 */
@Slf4j
@RestController
@RequestMapping("/sysDict")
public class SysDictController extends BaseController<SysDictEntity,SysDictVO,SysDictEO,SysDictQuery, SysDictMapper,SysDictService> {
    @Resource
    private SysDictService service;

    @ApiNote(name = "获取类型选项",createName = "keller",createTime = "2025-05-30 21:56:33")
    @GetMapping("/typeOptions")
    public R typeOptions(){
        return R.success(service.getTypeOptions());
    }

    @ApiNote(name = "获取字典树",createName = "keller",createTime = "2025-05-30 21:56:33")
    @GetMapping("/tree")
    public R tree(String type){
        SysDictQuery query = new SysDictQuery();
        query.setType(type);
        return R.success(service.baseQueryTree(query));
    }

    /**
     * 重写保存接口，更新缓存
     * @param vo
     * @return
     */
    @Override
    @PostMapping("/baseSave")
    @ApiNote(name="保存字典",createName = "keller",createTime = "2025-06-28 12:56:13")
    public R baseSave(@RequestBody SysDictVO vo){
        service.baseSaveVO(vo);
        DictCache.initCache();
        return R.success();
    }

    /**
     * 重写删除接口，更新缓存
     * @param vo
     * @return
     */
    @Override
    @PostMapping("/baseDelete")
    @ApiNote(name="删除字典",createName = "keller",createTime = "2025-06-28 12:56:18")
    public R deleteById(@RequestBody SysDictEntity vo){
        service.baseDeleteById(vo.getId());
        DictCache.initCache();
        return R.success();
    }
}